import axios from 'axios';

import {
  BASEURL
} from './config'
//自动设置服务器发送来的cookie
// axios.defaults.withCredentials=true
// 创建axios实例
axios.defaults.withCredentials = true;

const ajax = axios.create({
  baseURL: BASEURL,
  timeout: 10000 // 请求超时时间
});
// respone拦截器
ajax.interceptors.response.use(
  response => {
    if (response.data.code === 2005) {
      location.hash = '/login'
    } else if (response.data.code === 1) {
      return response.data
    } else {
      if(response.data.code !== 2004){
        $loading.hide();
        $toast.hide();
        setTimeout(() => {
            $toast.show(response.data.msg, 2000);
        }, 500);
      }
    }
  }, error => {
    // Message({
    //     message: error.message,
    //     type: 'error',
    //     duration: 1 * 1000
    // });
    return Promise.reject(error);
  }
)
export default ajax;
